// pages/exchange/exchange.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    goodsInfo: {},
    type: '',
    userScore: 0
  },

  addAddress: function () {
    let that = this
    wx.getSetting({
      success: res => {
        console.log("vres.authSetting['scope.address']：", res.authSetting['scope.address'])
        if (res.authSetting['scope.address']) {
          console.log("111")
          wx.chooseAddress({
            success(res) {
              console.log(res.userName)
              console.log(res.postalCode)
              console.log(res.provinceName)
              console.log(res.cityName)
              console.log(res.countyName)
              console.log(res.detailInfo)
              console.log(res.nationalCode)
              console.log(res.telNumber)
              that.setData({
                userInfo: res
              })
            }
          })
          // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问

        } else {
          if (res.authSetting['scope.address'] == false) {
            console.log("222")
            wx.openSetting({
              success(res) {
                console.log(res.authSetting)

              }
            })
          } else {
            console.log("eee")
            wx.chooseAddress({
              success(res) {
                console.log(res.userName)
                console.log(res.postalCode)
                console.log(res.provinceName)
                console.log(res.cityName)
                console.log(res.countyName)
                console.log(res.detailInfo)
                console.log(res.nationalCode)
                console.log(res.telNumber)
                let userInfo = res
                userInfo.address = `${res.provinceName}${res.cityName}${res.countyName}${res.detailInfo}`
                that.setData({
                  userInfo
                })
              }
            })
          }
        }
      }
    })
  },

  // // 修改姓名
  // userNameChange: function (e) {
  //   this.data.userInfo.userName = e.detail.value
  // },

  // // 修改电话
  // userTelChange: function (e) {
  //   this.data.userInfo.telNumber = e.detail.value
  // },

  // // 修改地址
  // addressChange: function (e) {
  //   this.data.userInfo.address = e.detail.value
  // },

  // 兑换  领取
  toExchange: function () {
    if (this.data.goodsInfo.goodsScore > this.data.userScore) {
      wx.showToast({
        title: '积分不足',
        icon: 'none'
      })
    } else {
      if (this.data.userInfo.userName) {
        if (this.data.type == 'receive') {
          app.post('Prize/receive', {
            token: wx.getStorageSync('token'),
            plid: this.data.goodsInfo.plid,
            name: this.data.userInfo.userName,
            phone: this.data.userInfo.telNumber,
            province: this.data.userInfo.provinceName,
            city: this.data.userInfo.cityName,
            area: this.data.userInfo.countyName,
            info: this.data.userInfo.detailInfo
          }).then(res => {
            wx.showModal({
              content: '领取成功',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          })
        } else {
          app.post('Goods/exchange', {
            token: wx.getStorageSync('token'),
            id: this.data.goodsInfo.id,
            label: this.data.goodsInfo.label,
            count: this.data.goodsInfo.count,
            name: this.data.userInfo.userName,
            phone: this.data.userInfo.telNumber,
            province: this.data.userInfo.provinceName,
            city: this.data.userInfo.cityName,
            area: this.data.userInfo.countyName,
            info: this.data.userInfo.detailInfo
          }).then(res => {
            wx.showModal({
              content: '兑换成功',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 2,
                  })
                }
              }
            })
          })
        }
      } else {
        wx.showToast({
          title: '请添加地址',
          icon: 'none'
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      wx.setNavigationBarTitle({
        title: '确认领取',
      })
      this.setData({
        type: options.type,
      })
    } else {
      wx.setNavigationBarTitle({
        title: '确认兑换',
      })
    }
    this.setData({
      goodsInfo: JSON.parse(options.goodsInfo),
    })


    app.post('Member/info', {
      token: wx.getStorageSync('token')
    }).then(res => {
      this.setData({
        userScore: res.data.result_data.m_score
      })
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})